<?php
declare (strict_types = 1);

namespace app\wxApi\controller;

use think\Exception;
use think\facade\Db;
use think\Request;

class Active
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        //
        try{
            $params =$request->get();
            if(isset($params['id'])&&!empty($params['id'])){
                $active  =Db::name('active')->find($params['id']);
                if(empty($active)){
                    throw new Exception('非法操作',9004);
                }
                $cache  =$request->cache;
                $examiner  =Db::name('examiner_baseinfo')->where('openid','=',$cache['openid'])->find();
                // print_r($cache);
                if(!in_array($examiner['examiner_id'],json_decode($active['examiner']))){
                    throw new Exception('没有执考权限',9004);
                }
                $condition=[
                    ['active_id','=',$params['id']],
                    ['status','<>',1]
                ];
                $field=["answer.*",'examinee_baseinfo.name','examinee_baseinfo.img',Db::raw("FROM_UNIXTIME(answer.create_time, '%Y-%m-%d %H:%i:%s') AS createTime")];
                $list = Db::name('answer')->alias('answer')->leftJoin('examinee_baseinfo','examinee_id=examinee_baseinfo.id')->where($condition)->field($field)->select()->toArray();
                foreach ($list as $key=>$item) {
                    if(empty($item['video_url'])&&!empty($item['video_local_url'])){
                        $list[$key]['video_url']='https://kj.cdfh2012.com/'.$item['video_local_url'];
                    }
                }
                return success($list);
            }else{
                throw new Exception('非法操作',9005);
            }
        }catch (\Exception $e){
            return error([],$e->getCode()?:9006,$e->getMessage());
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return Response
     */
    public function read(int $id)
    {
        //
        $details = Db::name('active')->alias('active')
            ->join('user','user.id=active.user_id')
            ->join('organ','user.id=organ.user_id')->field(['active.*','organ.name as organ'])->find($id);
        if(strtotime($details['apply_end'])>time()){
            $details['isValid']=1;
            return success($details);
        }else{
            $details['isValid']=0;
            return error($details,9001,'报名已截止');
        }

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
